//
//  YPInvestCell.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/1/6.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPInvestCell.h"
#import "YPBorrowTypeView.h"
#import "YRColorfulRoundProgressView.h"

@interface YPInvestCell (){
    
    UILabel *_titleLabel;
    YPBorrowTypeView *_borrowTypeView;
    YRColorfulRoundProgressView *_progressView; //进度条
    UILabel *_aprLabel; //年利率
    UILabel *_timeLimitLabel; //期限
    UILabel *_totalMoney; //标的总额
    UIImageView *_awardImageV; //奖励
    UILabel *_awardLabel; //奖励
    UILabel *_cycleLabel; //进度
}

@end

@implementation YPInvestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        CGFloat fontSize = (iPhone4 || iPhone5)?13:15;
        _titleLabel = [UILabel creatWithFrame:CGRectMake(autoScaleW(15), 10, fontSize*13+10, 20)
                                         font:[UIFont systemFontOfSize:fontSize]
                                         text:@""
                                    textColor:[UIColor blackColor]
                                textAlignment:0];
        [self.contentView addSubview:_titleLabel];
        
        
        
        _borrowTypeView = [[YPBorrowTypeView alloc]initWithFrame:CGRectMake(_titleLabel.right + 5, _titleLabel.top , 0, _titleLabel.height)];
        [self.contentView addSubview:_borrowTypeView];
        
        
        
        UIImage *awardImage = [UIImage imageNamed:@"jiangli"];
        _awardImageV = [[UIImageView alloc]initWithImage:awardImage];
        [_awardImageV sizeToFit];
        _awardImageV.top = _borrowTypeView.top + 4;
        [self.contentView addSubview:_awardImageV];
        _awardLabel = [UILabel creatWithFrame:CGRectMake(17, 0, _awardImageV.width - 17, _awardImageV.height)
                                         font:[UIFont systemFontOfSize:10]
                                         text:@""
                                    textColor:[UIColor whiteColor]
                                textAlignment:0];
        [_awardImageV addSubview:_awardLabel];
        
        
        CGFloat space = 15;
        CGFloat aprWidth = (SCREEN_WIDTH - _titleLabel.left * 2)/3.f;
        CGFloat cycleW = 60;
        CGFloat middleW = SCREEN_WIDTH - _titleLabel.left * 2 - aprWidth - cycleW - space;
        _aprLabel = [UILabel creatWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom + 20, aprWidth, 20)
                                       font:[UIFont systemFontOfSize:12]
                                       text:@""
                                  textColor:kColor_AppNavColor
                              textAlignment:0];
        [self.contentView addSubview:_aprLabel];
        UILabel *apr = [UILabel creatWithFrame:CGRectMake(_aprLabel.left, _aprLabel.bottom + 5, aprWidth, 20)
                                          font:[UIFont systemFontOfSize:12]
                                          text:@"参考年化收益率"
                                     textColor:kColor_greyAshTitleColor
                                 textAlignment:0];
        [self.contentView addSubview:apr];
        
        
        
        UILabel *timeLimit = [UILabel creatWithFrame:CGRectMake(_aprLabel.right + space , _aprLabel.top, 0, apr.height)
                                                font:apr.font
                                                text:@"期限"
                                           textColor:kColor_greyAshTitleColor
                                       textAlignment:0];
        [self.contentView addSubview:timeLimit];
        timeLimit.width = [timeLimit.text widthWithFont:timeLimit.font height:timeLimit.height];
        _timeLimitLabel = [UILabel creatWithFrame:CGRectMake(timeLimit.right + 10, timeLimit.top, middleW - timeLimit.width - 10, _aprLabel.height)
                                             font:[UIFont systemFontOfSize:15]
                                             text:@""
                                        textColor:[UIColor blackColor]
                                    textAlignment:0];
        [self.contentView addSubview:_timeLimitLabel];
        
        
        
        
        UILabel *totalMoney = [UILabel creatWithFrame:CGRectMake(timeLimit.left, apr.top, timeLimit.width, timeLimit.height)
                                                 font:timeLimit.font
                                                 text:@"总额"
                                            textColor:kColor_greyAshTitleColor
                                        textAlignment:0];
        [self.contentView addSubview:totalMoney];
        _totalMoney = [UILabel creatWithFrame:CGRectMake(_timeLimitLabel.left, totalMoney.top, _timeLimitLabel.width, _aprLabel.height)
                                         font:[UIFont systemFontOfSize:15]
                                         text:@""
                                    textColor:[UIColor blackColor]
                                textAlignment:0];
        [self.contentView addSubview:_totalMoney];
        
        
        
        _progressView = [[YRColorfulRoundProgressView alloc] initWithFrame:CGRectMake(_totalMoney.right, _aprLabel.top - 10, cycleW, cycleW)];
        [self.contentView addSubview:_progressView];
        
        _cycleLabel = [UILabel creatWithFrame:CGRectMake(5, (cycleW - 20)/2.f, cycleW - 12, 20)
                                         font:[UIFont systemFontOfSize:11]
                                         text:@""
                                    textColor:[UIColor darkGrayColor]
                                textAlignment:1];
        [_progressView addSubview:_cycleLabel];
        
        
        
        
        [self.contentView addLineWithFrame:CGRectMake(_aprLabel.right, _aprLabel.top + 5 , 0.5, 35) color:colorWithRGB(230, 230, 230)];
        
    }
    
    return self;
}

- (void)setModel:(YPInvestModel *)model{
    
    _model = model;
    
    _titleLabel.text = model.title;
    CGFloat titleW = [_titleLabel.text widthWithFont:_titleLabel.font height:_titleLabel.height];
    if (titleW <= _titleLabel.width) {
        _borrowTypeView.left = _titleLabel.left + titleW + 5;
    }else{
        _borrowTypeView.left = _titleLabel.right + 5;
    }
    _timeLimitLabel.text = [NSString stringWithFormat:@"%@个月",_model.time_limit];
    _aprLabel.attributedText = [AppTools getAttributedStringWithRate:model.apr rateAdd:model.apr_add fontSize:25];
    _totalMoney.text = _model.account;
    [_borrowTypeView setBorrowType:model.borrow_type];
    if (model.award.floatValue > 0) {
        _awardImageV.hidden = NO;
        _awardImageV.left = _borrowTypeView.right - 2;
        _awardLabel.text = [NSString stringWithFormat:@"%@%%",model.award];
    }else{
        _awardImageV.hidden = YES;
    }
    
    _progressView.progress = [model.progress_bar floatValue];
    _cycleLabel.text = [NSString stringWithFormat:@"%@%%",model.progress_bar];
    if (model.progress_bar.integerValue ==100) {
        _cycleLabel.text = [NSString stringWithFormat:@"%ld%%",(long)model.progress_bar.integerValue];
        
    }

    
}
@end
